from django.shortcuts import render,redirect,reverse
from gold import models
# Create your views here.
import math
def add_goods(request):
    if request.method=='POST':
        #获取表单提交的内容
        goods_name = request.POST.get('goods_name')
        goods_type = request.POST.get('goods_type')
        goods_price = request.POST.get('goods_price')
        unit = request.POST.get('unit')
        remarks = request.POST.get('remarks')

        #保存到数据库
        models.Goods_info.objects.create(goods_name=goods_name,goods_type=goods_type,goods_price=goods_price,unit=unit,remarks=remarks)
        return redirect('/gold/goods_list')
    return render(request,'add_goods.html')


def goods_list(request):
    #查询数据库中所有数据
    goods_list = models.Goods_info.objects.filter(delete_flag=0)
    print(goods_list)
    return render(request,'goods_list.html',{'goods_list':goods_list})


def update_goods(request):
    if request.method=='POST':
        id = request.POST.get('id')
        goods_name = request.POST.get('goods_name')
        goods_type = request.POST.get('goods_type')
        goods_price = request.POST.get('goods_price')
        unit = request.POST.get('unit')
        remarks = request.POST.get('remarks')

        goods_obj = models.Goods_info.objects.get(id=id)
        goods_obj.goods_name = goods_name
        goods_obj.goods_type = goods_type
        goods_obj.goods_price = goods_price
        goods_obj.unit = unit
        goods_obj.remarks = remarks
        goods_obj.save()
        return redirect('/gold/goods_list')



    else:

        id = request.GET.get('id')
        print(id,'-----------------')
        goods_obj = models.Goods_info.objects.get(id=id)
        goods_list = models.Goods_info.objects.all()

        return render(request,'update_goods.html',{'goods_list':goods_list,'goods_obj':goods_obj})


def delete_goods(request):
    id = request.GET.get('id')
    print(id, '-----------------')
    goods_obj = models.Goods_info.objects.get(id=id)
    goods_obj.delete_flag = 1
    goods_obj.save()
    return redirect('/gold/goods_list')

import datetime

def customer_list(request):
    # 查询数据库中所有数据
    customer_list = models.Customer_info.objects.filter(delete_flag=0)
    for customer_obj in customer_list:
        customer_obj.order_time = datetime.datetime.strftime(customer_obj.order_time, '%Y-%m-%d')
    print(customer_list)
    return render(request, 'customer_list.html', {'customer_list': customer_list})


def add_customer(request):
    if request.method == 'POST':
        # 获取表单提交的内容
        order_time = request.POST.get('order_time')
        order_num = request.POST.get('order_num')
        customer_name = request.POST.get('customer_name')
        customer_phone = request.POST.get('customer_phone')
        customer_adr = request.POST.get('customer_adr')
        deal_money = request.POST.get('deal_money')
        deposit_money = request.POST.get('deposit_money')
        rest_money = int(deal_money)-int(deposit_money)
        ruler_flag = request.POST.get('ruler_flag')
        delivery_flag = request.POST.get('delivery_flag')
        delivery_time = request.POST.get('delivery_time')
        clean_status = request.POST.get('clean_status')
        remarks = request.POST.get('remarks')

        # 保存到数据库
        models.Customer_info.objects.create(order_time=order_time, order_num=order_num, customer_name=customer_name,
                                         customer_phone=customer_phone, customer_adr=customer_adr,deal_money=deal_money,deposit_money=deposit_money,rest_money=rest_money,
                                         ruler_flag=ruler_flag,delivery_flag=delivery_flag,delivery_time=delivery_time,clean_status=clean_status,remarks=remarks)
        return redirect('/gold/customer_list')
    return render(request, 'add_customer.html')


def update_customer(request):
    if request.method =='POST':
        id = request.POST.get('id')
        order_time = request.POST.get('order_time')
        order_num = request.POST.get('order_num')
        customer_name = request.POST.get('customer_name')
        customer_phone = request.POST.get('customer_phone')
        deal_money = request.POST.get('deal_money')
        deposit_money = request.POST.get('deposit_money')
        rest_money = int(deal_money)-int(deposit_money)
        ruler_flag = request.POST.get('ruler_flag')
        delivery_flag = request.POST.get('delivery_flag')
        delivery_time = request.POST.get('delivery_time')
        clean_status = request.POST.get('clean_status')
        remarks = request.POST.get('remarks')

        customer_obj = models.Customer_info.objects.get(id=id)
        customer_obj.order_time = order_time
        customer_obj.order_num = order_num
        customer_obj.customer_name = customer_name
        customer_obj.customer_phone = customer_phone
        customer_obj.deal_money = deal_money
        customer_obj.deposit_money = deposit_money
        customer_obj.rest_money = rest_money
        customer_obj.ruler_flag = ruler_flag
        customer_obj.delivery_flag = delivery_flag
        customer_obj.delivery_time = delivery_time
        customer_obj.clean_status = clean_status
        customer_obj.remarks = remarks
        customer_obj.save()

        return redirect('/gold/customer_list')

    else:
        id = request.GET.get('id')
        customer_obj = models.Customer_info.objects.get(id=id)
        customer_obj.order_time = datetime.datetime.strftime(customer_obj.order_time,'%Y-%m-%d')
        customer_list = models.Customer_info.objects.all()
        return render(request,'update_customer.html',{'customer_list':customer_list,'customer_obj':customer_obj})



def delete_customer(request):
    id = request.GET.get('id')
    customer_obj = models.Customer_info.objects.get(id=id)
    customer_obj.delete_flag = 1
    customer_obj.save()
    return redirect('/gold/customer_list')


def order_list(request):
    id = request.GET.get('id')
    customer_obj = models.Customer_info.objects.get(id=id)
    order_list = models.Order_detail.objects.filter(customer_id=id,delete_flag=0)

    total = 0

    for order_obj in order_list:
        order_obj.deal_total = order_obj.deal_unit * order_obj.area
        total += order_obj.deal_total



    return render(request,'order_list.html',{'customer_obj':customer_obj,'order_list':order_list,'goods_list':goods_list,'total':total})


def add_order(request):
    if request.method =="POST":
        customer_id_id = request.POST.get('customer_id')


        space_name = request.POST.get('space_name')
        goods_id_id = request.POST.get('goods_id_id')

        deal_unit = request.POST.get('deal_unit')
        area = request.POST.get('area')
        goods_nums = request.POST.get('goods_nums')
        remarks = request.POST.get('remarks')
        deal_total = (int(deal_unit)*int(area))


        models.Order_detail.objects.create(customer_id_id=customer_id_id,space_name=space_name,goods_id_id=goods_id_id,deal_unit=deal_unit
                                           ,area=area,goods_nums=goods_nums,remarks=remarks,deal_total=deal_total)



        return redirect('/gold/order_list?id=%s' %(customer_id_id))
    else:
        customer_id = request.GET.get('id')

        goods_list = models.Goods_info.objects.filter(delete_flag=0)
        return render(request,'add_order.html',{'goods_list':goods_list,'customer_id':customer_id})


def update_order(request):
    if request.method=='POST':
        id = request.POST.get('id')
        customer_id_id = request.POST.get('customer_id_id')
        space_name = request.POST.get('space_name')
        goods_id_id = request.POST.get('goods_id_id')
        deal_unit = request.POST.get('deal_unit')
        area = request.POST.get('area')
        print(type(area),'----------=-=-=-=-=-=-=-=-=-')
        goods_nums = request.POST.get('goods_nums')
        remarks = request.POST.get('remarks')
        deal_total = float(deal_unit) * float(area)

        order_obj = models.Order_detail.objects.get(id=id)
        order_obj.customer_id_id = customer_id_id
        order_obj.space_name = space_name
        order_obj.goods_id_id = goods_id_id
        order_obj.deal_unit = deal_unit
        order_obj.area = area
        order_obj.goods_nums = goods_nums
        order_obj.deal_total = deal_total
        order_obj.remarks = remarks
        order_obj.save()


        return redirect('/gold/order_list?id=%s' %(customer_id_id))
    else:
        order_id = request.GET.get('id')

        order_obj = models.Order_detail.objects.get(id=order_id,delete_flag=0)

        goods_list = models.Goods_info.objects.filter(delete_flag=0)
        return render(request,'update_order.html',{'goods_list':goods_list,'order_obj':order_obj})


def delete_order(request):
    id = request.GET.get('id')
    order_obj = models.Order_detail.objects.get(id=id)
    order_obj.delete_flag = 1
    order_obj.save()
    return redirect('/gold/order_list?id=%s' %(order_obj.customer_id_id))


def index(request):
    pass